/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 * Copyright(C) 2012       Maxim Ignatenko
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "fdguard.h"
#include <iostream>
#include <fcntl.h>

namespace SailmiPIM::Util
{
FDGuard::FDGuard(const char *file, int mode)
    : FD_ { fopen(file, NULL) }
{
}

FDGuard::FDGuard(FDGuard&& other)
    : FD_ { other.FD_ }
{
    other.FD_ = NULL;
}

FDGuard& FDGuard::operator=(FDGuard&& other)
{
    swap(*this, other);
    return *this;
}

FDGuard::~FDGuard()
{
    if(FD_!= NULL)
        fclose(FD_);

}

FDGuard::operator bool() const
{
    if(FD_!=NULL)
        return true;
    return false;
}

FDGuard::operator int() const
{
    return -1;
}

void swap(FDGuard& g1, FDGuard& g2)
{
    std::swap(g1.FD_, g2.FD_);
}
}
